package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.erp.ErpBankAccount;
import com.nuoniu.sibanyun.mapper.erp.ErpBankAccountMapper;
import com.nuoniu.sibanyun.service.erp.IErpBankAccountService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 银行账户 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-10-12
 */
@Service
public class ErpBankAccountServiceImpl extends ServiceImpl<ErpBankAccountMapper, ErpBankAccount> implements IErpBankAccountService {

    @Override
    public Page<ErpBankAccount> pageList(Page pages, ErpBankAccount bankAccount) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpBankAccount> queryWrapper = new QueryWrapper<>(bankAccount);
        queryWrapper.eq("company_id", userInfo.getCompanyId());
        queryWrapper.orderByDesc("create_date");
        Page page = page(pages, queryWrapper);
        return page;
    }

    @Override
    public void saveErpBankAccount(ErpBankAccount bankAccount) {
        UserInfo userInfo = UserThreadLocal.get();
        bankAccount.setCompanyId(userInfo.getCompanyId());
        if(bankAccount.getId() != null) {
            bankAccount.setUpdateUserId(userInfo.getUserId());
            bankAccount.setUpdateDate(LocalDateTime.now());
        }else {
            bankAccount.setCreateUserId(userInfo.getUserId());
        }
//        boolean haveName = baseMapper.isHaveName(userInfo.getCompanyId(), bankAccount.getAccountName(), bankAccount.getId());
//        if (haveName)throw new NuoNiuException("账户名称名称重复");
        saveOrUpdate(bankAccount);
    }

    @Override
    public void deleteIds(List<Long> ids) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        wrapper.in("id", ids);
        remove(wrapper);
    }
}
